// pages/cate/cate.js
const util = require('../../utils/util.js');
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    topCate: [],
    cates: [{ id: 8, cate_name: "定制游" }, { id: 24, cate_name: "露营之旅" }, { id: 23, cate_name: "朝圣之旅" }, { id: 18, cate_name: "艺术之旅" }, { id: 17, cate_name: "游学之旅" }, { id: 16, cate_name: "慈善之旅" }, { id: 15, cate_name: "吃货之旅" }, { id: 11, cate_name: "群体主题" }, { id: 6, cate_name: "境内游" }, { id: 7, cate_name: "境外游" }, { id: 25, cate_name: "高峰体验" }],
    sonCates: [],
    startCateId: 8
  },


  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // 请求顶级栏目数据
    this.getTopCates();
    //  请求当前栏目数据
    this.getCateInfo();
    //  请求当前栏目子分类数据
    this.getSonCates();
  },

  //点击跳转到商品列表页
  goodslist: function (e) {
    var cid = e.currentTarget.dataset.cid;
    wx.navigateTo({
      url: '../goodslist/goodslist?cid=' + cid
    })
  },
  /**
   * 切换顶级栏目高亮状态并获取数据
   */
  changeid: function (e) {
    var that = this
    var curId = e.target.dataset.id;
    that.setData({ startCateId: curId });
    //  请求当前栏目数据
    this.getCateInfo();
    //  请求当前栏目子栏目数据
    this.getSonCates();
  },

  //获取所有的顶级栏目
  getTopCates: function () {
    var url = app.globalData.domain + '/cate/get_top_cate';
    var params = {};
    util.wxRequest(url, params, data => {
      // console.log(data);
      this.setData({
        topCate: data.data
      })
    }, data => { }, data => { });
  },
  //获取当前栏目信息 初始信息
  getCateInfo: function () {
    var startCateId = this.data.startCateId
    // console.log(startCateId);
    var url = app.globalData.domain + '/cate/get_cate_info';
    var params = { id: startCateId };
    util.wxRequest(url, params, data => {
      // console.log(data);
      this.setData({
        // cates: data.data
      })
    }, data => { }, data => { });
  },


  //  请求当前栏目子分类数据
  getSonCates: function () {
    var startCateId = this.data.startCateId
    var url = app.globalData.domain + '/cate/get_son_cates';
    var params = { pid: startCateId };
    util.wxRequest(url, params, data => {
      // console.log(data);
      this.setData({
        sonCates: data.data
      })
    }, data => { }, data => { });
  },
})
